package com.sl.au.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.Chance;
import com.sl.au.entity.ChanceDelivery;
import com.sl.au.entity.ChanceManagerDelivery;
import com.sl.au.entity.User;
import com.sl.au.service.ChanceManagerDeliveryService;
import com.sl.au.service.OperateLogService;

@RestController
@RequestMapping(value = "/ui/chanceReceive")
public class ChanceReceiveController extends BaseController {

	@Autowired
	ChanceManagerDeliveryService chanceManagerDeliveryService;

	@Autowired
	OperateLogService operateLogService; 

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("chanceReceive/chanceReceiveindex");
		return modelAndView;
	}

	/**
	 * 待接收信息分页数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getpage", method = RequestMethod.GET)
	public Map<String, Object> getPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> condition = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		String targetType = request.getParameter("targetType");
		if(key == null){key ="";}
		Account a = this.getMyAccount();
		String userId = a.getUser().getId();
		condition.put("targetType", targetType);
		condition.put("userId", userId);
		condition.put("isReceived", 0);
		condition.put("key", key);
		List<ChanceManagerDelivery> saleChances = chanceManagerDeliveryService.getPage(start, length, condition, "createTime",
				"desc");
		int count = chanceManagerDeliveryService.getPageCount(condition);
		map.put("data", saleChances);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	/**
	 * 员工已接收信息分页数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getMyChance", method = RequestMethod.GET)
	public Map<String, Object> getMyChance(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> condition = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		String isSucceed = request.getParameter("isSucceed");
		
		if(key == null){key ="";}
		Account a = this.getMyAccount();
		String userId = a.getUser().getId();
		condition.put("isSucceed", isSucceed);
		
		condition.put("userId", userId);
		condition.put("isReceived", 1);
		condition.put("key", key);
		List<ChanceManagerDelivery> saleChances = chanceManagerDeliveryService.getPage(start, length, condition, "createTime",
				"desc");
		int count = chanceManagerDeliveryService.getPageCount(condition);
		map.put("data", saleChances);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	/**
	 * 接收信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/receive", method = RequestMethod.GET)
	public Map<String, Object> receiveChance(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String managerDeliveryIds = request.getParameter("managerDeliveryIds");
		String id[] = managerDeliveryIds.split(",");
		Account a = this.getMyAccount();
		User staff = a.getUser();
		for (int i = 0; i < id.length; i++) {
			String managerDeliveryId = id[i];
			if (managerDeliveryId != null || managerDeliveryId != "") {
				ChanceManagerDelivery chanceManagerDelivery = chanceManagerDeliveryService.getRepository()
						.findOne(managerDeliveryId);
				if (chanceManagerDelivery != null) {
					chanceManagerDelivery.setIsReceived(1);
					chanceManagerDeliveryService.getRepository().save(chanceManagerDelivery);
					// 增加操作日志
					Chance chance = chanceManagerDelivery.getSaleChance();
					String content = "接收销售信息【" + chance.getName() + "】";
					operateLogService.log(staff, "信息接收", content);
				}
			}
		}
		map.put("code", 0);
		return map;
	}
   /**
    * 移动端获得未接受信息数量
    * 
    * **/
	@RequestMapping(value = "/getNoReceiveChanceCount", method = RequestMethod.GET)
	public Map<String, Object> getNoReceiveChanceCount(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> condition = new HashMap<String, Object>();
		String key = request.getParameter("key");
		if(key == null){key ="";}
		Account a = this.getMyAccount();
		String userId = a.getUser().getId();
		condition.put("userId", userId);
		condition.put("isReceived", 0);
		condition.put("key", key);
		int count = chanceManagerDeliveryService.getPageCount(condition);
		map.put("data", count);
		return map;
	}
	   /**
	    * 移动端获得已接受信息数量
	    * 
	    * **/
		@RequestMapping(value = "/getReceiveChanceCount", method = RequestMethod.GET)
		public Map<String, Object> getReceiveChanceCount(HttpServletRequest request) {
			Map<String, Object> map = new HashMap<String, Object>();
			Map<String, Object> condition = new HashMap<String, Object>();
			String key = request.getParameter("key");
			if(key == null){key ="";}
			Account a = this.getMyAccount();
			String userId = a.getUser().getId();
			condition.put("userId", userId);
			condition.put("isReceived", 1);
			condition.put("key", key);
			int count = chanceManagerDeliveryService.getPageCount(condition);
			map.put("data", count);
			return map;
		}
		/**
		 * 移动端接收信息
		 * 
		 * @return
		 */
		@RequestMapping(value = "/phoneReceive", method = RequestMethod.GET)
		public Map<String, Object> phoneReceive(HttpServletRequest request) {
			Map<String, Object> map = new HashMap<String, Object>();
			String id = request.getParameter("id");
			Account a = this.getMyAccount();
			User staff = a.getUser();
				if (id != null || id != "") {
					ChanceManagerDelivery chanceManagerDelivery = chanceManagerDeliveryService.getRepository()
							.findOne(id);
					if (chanceManagerDelivery != null) {
						chanceManagerDelivery.setIsReceived(1);
						chanceManagerDeliveryService.getRepository().save(chanceManagerDelivery);
						// 增加操作日志
						Chance chance = chanceManagerDelivery.getSaleChance();
						String content = "接收销售信息【" + chance.getName() + "】";
						operateLogService.log(staff, "信息接收", content);
					}
			}
			map.put("code", 0); 
			return map;
		}
}
